<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="微信支付SDK下载 接入指南 继承步骤 项目中导入SDK">
<meta property="og:type" content="article">
<meta property="og:title" content="WechatPay">
<meta property="og:url" content="http://yoursite.com/2018/02/17/WechatPay/index.html">
<meta property="og:site_name" content="Erhqe">
<meta property="og:description" content="微信支付SDK下载 接入指南 继承步骤 项目中导入SDK">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://ws2.sinaimg.cn/large/006tKfTcly1g0bttdqq4nj304z03x3yi.jpg">
<meta property="og:image" content="https://ws4.sinaimg.cn/large/006tKfTcly1g0bzyfm1iwj30ol051t8z.jpg">
<meta property="og:image" content="https://ws2.sinaimg.cn/large/006tKfTcly1g0bzmfbv8gj312h0bmtei.jpg">
<meta property="og:updated_time" content="2019-07-19T05:40:06.067Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="WechatPay">
<meta name="twitter:description" content="微信支付SDK下载 接入指南 继承步骤 项目中导入SDK">
<meta name="twitter:image" content="https://ws2.sinaimg.cn/large/006tKfTcly1g0bttdqq4nj304z03x3yi.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/02/17/WechatPay/"/>





  <title>WechatPay | Erhqe</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Erhqe</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/02/17/WechatPay/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Erhqe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/10.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Erhqe">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">WechatPay</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-02-17T13:31:20+08:00">
                2018-02-17
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Pay/" itemprop="url" rel="index">
                    <span itemprop="name">Pay</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2018/02/17/WechatPay/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2018/02/17/WechatPay/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="微信支付"><a href="#微信支付" class="headerlink" title="微信支付"></a>微信支付</h3><p><a href="https://res.wx.qq.com/op_res/_bwkvGV3R1XHqZeRgGTd6YHyvEZgBJdv95a2NXVRqHg5sPzhuAghRrQA9bzienfF" target="_blank" rel="noopener"><strong>SDK下载</strong></a></p>
<p><strong><a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&amp;t=resource/res_list&amp;verify=1&amp;id=1417694084&amp;token=&amp;lang=zh_CN" target="_blank" rel="noopener">接入指南</a></strong></p>
<p><a href="https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=11_1" target="_blank" rel="noopener"><strong>继承步骤</strong></a></p>
<p><strong>项目中导入SDK</strong></p>
<p><img src="https://ws2.sinaimg.cn/large/006tKfTcly1g0bttdqq4nj304z03x3yi.jpg" alt=""><a id="more"></a></p>
<hr>
<p><strong>白名单内添加微信</strong></p>
<p><strong>LSApplicationQueriesSchemes[@”weixin”,@”wechat”]</strong></p>
<hr>
<p><strong>设置URLTyples(商户自己APPID)</strong></p>
<p><img src="https://ws4.sinaimg.cn/large/006tKfTcly1g0bzyfm1iwj30ol051t8z.jpg" alt=""></p>
<hr>
<p><strong>测试项目</strong></p>
<p><strong><img src="https://ws2.sinaimg.cn/large/006tKfTcly1g0bzmfbv8gj312h0bmtei.jpg" alt=""></strong></p>
<hr>
<p><strong>APPDlegate</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">#import &quot;AppDelegate.h&quot;</span><br><span class="line">#import &quot;WXApi.h&quot;</span><br><span class="line">#import &quot;WXApiManager.h&quot;</span><br><span class="line">@interface AppDelegate ()</span><br><span class="line"></span><br><span class="line">@end</span><br><span class="line"></span><br><span class="line">@implementation AppDelegate</span><br><span class="line"></span><br><span class="line">- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions &#123;</span><br><span class="line">    [WXApi registerApp:@&quot;appID&quot;];</span><br><span class="line">    return YES;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> 这里处理微信/支付宝支付完成之后跳转回来</span><br><span class="line"> */</span><br><span class="line">- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation</span><br><span class="line">&#123;</span><br><span class="line">    </span><br><span class="line">    // 微信的支付回调</span><br><span class="line">    if ([url.host isEqualToString:@&quot;pay&quot;]) &#123;</span><br><span class="line">        [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];</span><br><span class="line">    &#125;</span><br><span class="line">    return YES;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">// NOTE: 9.0以后使用新API接口</span><br><span class="line">- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary&lt;NSString*, id&gt; *)options</span><br><span class="line">&#123;</span><br><span class="line">    </span><br><span class="line">    // 微信的支付回调</span><br><span class="line">    if ([url.host isEqualToString:@&quot;pay&quot;]) &#123;</span><br><span class="line">        return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    return YES;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>支付按钮点击事件</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">- (IBAction)payClick:(id)sender &#123;</span><br><span class="line">    //  通过自己后台请求订单信息</span><br><span class="line">    //  1参数</span><br><span class="line">    NSMutableDictionary *parameter = [NSMutableDictionary dictionary];</span><br><span class="line">    parameter[@&quot;orderid&quot;] = [self generateTradeNO];</span><br><span class="line">    parameter[@&quot;userIp&quot;] = [self deviceIPAdress];</span><br><span class="line">    </span><br><span class="line">    //  2发送请求</span><br><span class="line">    NSURLSession *session = [NSURLSession sharedSession];</span><br><span class="line">    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@&quot;URL&quot;]];</span><br><span class="line">    request.HTTPMethod = @&quot;POST&quot;;</span><br><span class="line">    request.HTTPBody = [NSJSONSerialization dataWithJSONObject:parameter options:NSJSONWritingPrettyPrinted error:nil];</span><br><span class="line">    //创建任务Task</span><br><span class="line">   __block NSMutableDictionary *responseDict  = [NSMutableDictionary dictionary];</span><br><span class="line">    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) &#123;</span><br><span class="line">        responseDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];</span><br><span class="line">    &#125;];</span><br><span class="line">    [task resume];</span><br><span class="line">    </span><br><span class="line">    //  3订单信息</span><br><span class="line">    [WXApi registerApp:[responseDict objectForKey:@&quot;appid&quot;]];</span><br><span class="line">    PayReq *rep = [[PayReq alloc]init];</span><br><span class="line">    rep.partnerId = [responseDict objectForKey:@&quot;mch_id&quot;];  // 商家向财付通申请的商家id</span><br><span class="line">    rep.prepayId = [responseDict objectForKey:@&quot;prepay_id&quot;]; // 支付订单</span><br><span class="line">    rep.package = @&quot;Sign=WXPay&quot;;  // Sign=WXPay 商家根据财付通文档填写的数据和签名</span><br><span class="line">    rep.nonceStr = [responseDict objectForKey:@&quot;nonce_str&quot;]; // 随机串，防重发</span><br><span class="line">    rep.timeStamp = [[responseDict objectForKey:@&quot;timestamp&quot;] intValue]; // 时间戳，防重发</span><br><span class="line">    rep.sign = [responseDict objectForKey:@&quot;sign2&quot;]; // 商家根据微信开放平台文档对数据做的签名 二次签名</span><br><span class="line">    </span><br><span class="line">    //  3 BOOL = [WXApi sendReq:rep] 会切换到微信的界面。第三方应用程序等待微信返回onResp。微信在异步处理完成后一定会调用onResp。</span><br><span class="line">    if ([WXApi sendReq:rep]) &#123;</span><br><span class="line">        //  这里的通知在</span><br><span class="line">        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(succeed) name:@&quot;WeChatPaySucceed&quot; object:nil];</span><br><span class="line">        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(fail) name:@&quot;WeChatPayFail&quot; object:nil];</span><br><span class="line">    &#125;else&#123;</span><br><span class="line">        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@&quot;支付失败&quot; message:@&quot;未安装微信客户端,请使用其他支付方式&quot; preferredStyle:UIAlertControllerStyleAlert];</span><br><span class="line">        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@&quot;确定&quot; style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) &#123;</span><br><span class="line">            </span><br><span class="line">        &#125;];</span><br><span class="line">        [alert addAction:okAction];</span><br><span class="line">        [self presentViewController:alert animated:YES completion:nil];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>通知</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">#pragma mark - succeed</span><br><span class="line">- (void)succeed&#123;</span><br><span class="line">    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@&quot;测试&quot; message:@&quot;支付成功&quot; preferredStyle:UIAlertControllerStyleAlert];</span><br><span class="line">    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@&quot;确定&quot; style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) &#123;</span><br><span class="line">        </span><br><span class="line">    &#125;];</span><br><span class="line">    [alert addAction:okAction];</span><br><span class="line">    [self presentViewController:alert animated:YES completion:nil];</span><br><span class="line">&#125;</span><br><span class="line">#pragma mark - fail</span><br><span class="line">- (void)fail&#123;</span><br><span class="line">    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@&quot;测试&quot; message:@&quot;支付失败&quot; preferredStyle:UIAlertControllerStyleAlert];</span><br><span class="line">    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@&quot;确定&quot; style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) &#123;</span><br><span class="line">        </span><br><span class="line">    &#125;];</span><br><span class="line">    [alert addAction:okAction];</span><br><span class="line">    [self presentViewController:alert animated:YES completion:nil];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>获取IP</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">#pragma mark - 获取用户IP</span><br><span class="line">- (NSString *)deviceIPAdress &#123;</span><br><span class="line">    NSString *address = @&quot;an error occurred when obtaining ip address&quot;;</span><br><span class="line">    struct ifaddrs *interfaces = NULL;</span><br><span class="line">    struct ifaddrs *temp_addr = NULL;</span><br><span class="line">    int success = 0;</span><br><span class="line">    </span><br><span class="line">    success = getifaddrs(&amp;interfaces);</span><br><span class="line">    </span><br><span class="line">    if (success == 0) &#123; // 0 表示获取成功</span><br><span class="line">        </span><br><span class="line">        temp_addr = interfaces;</span><br><span class="line">        while (temp_addr != NULL) &#123;</span><br><span class="line">            if( temp_addr-&gt;ifa_addr-&gt;sa_family == AF_INET) &#123;</span><br><span class="line">                // Check if interface is en0 which is the wifi connection on the iPhone</span><br><span class="line">                if ([[NSString stringWithUTF8String:temp_addr-&gt;ifa_name] isEqualToString:@&quot;en0&quot;]) &#123;</span><br><span class="line">                    // Get NSString from C String</span><br><span class="line">                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr-&gt;ifa_addr)-&gt;sin_addr)];</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            </span><br><span class="line">            temp_addr = temp_addr-&gt;ifa_next;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    freeifaddrs(interfaces);</span><br><span class="line">    </span><br><span class="line">    NSLog(@&quot;手机设备的IP是：%@&quot;, address);</span><br><span class="line">    return address;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>随机订单ID</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">#pragma mark - 订单ID（由商家自行制定）</span><br><span class="line">- (NSString *)generateTradeNO</span><br><span class="line">&#123;</span><br><span class="line">    static int kNumber = 15;</span><br><span class="line">    </span><br><span class="line">    NSString *sourceStr = @&quot;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;;</span><br><span class="line">    NSMutableString *resultStr = [[NSMutableString alloc] init];</span><br><span class="line">    srand((unsigned)time(NULL));</span><br><span class="line">    for (int i = 0; i &lt; kNumber; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        unsigned index = rand() % [sourceStr length];</span><br><span class="line">        NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];</span><br><span class="line">        [resultStr appendString:oneStr];</span><br><span class="line">    &#125;</span><br><span class="line">    return resultStr;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>创建WXApiManager文件遵循协议</strong></p>
<p><strong>.H</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">#import &lt;Foundation/Foundation.h&gt;</span><br><span class="line">#import &quot;WXApi.h&quot;</span><br><span class="line"></span><br><span class="line">@protocol WXApiManagerDelegate &lt;NSObject&gt;</span><br><span class="line"></span><br><span class="line">@optional</span><br><span class="line"></span><br><span class="line">- (void)managerDidRecvGetMessageReq:(GetMessageFromWXReq *)request;</span><br><span class="line"></span><br><span class="line">- (void)managerDidRecvShowMessageReq:(ShowMessageFromWXReq *)request;</span><br><span class="line"></span><br><span class="line">- (void)managerDidRecvLaunchFromWXReq:(LaunchFromWXReq *)request;</span><br><span class="line"></span><br><span class="line">- (void)managerDidRecvMessageResponse:(SendMessageToWXResp *)response;</span><br><span class="line"></span><br><span class="line">- (void)managerDidRecvAuthResponse:(SendAuthResp *)response;</span><br><span class="line"></span><br><span class="line">- (void)managerDidRecvAddCardResponse:(AddCardToWXCardPackageResp *)response;</span><br><span class="line"></span><br><span class="line">@end</span><br><span class="line"></span><br><span class="line">@interface WXApiManager : NSObject&lt;WXApiDelegate&gt;</span><br><span class="line"></span><br><span class="line">@property (nonatomic, assign) id&lt;WXApiManagerDelegate&gt; delegate;</span><br><span class="line"></span><br><span class="line">+ (instancetype)sharedManager;</span><br></pre></td></tr></table></figure>
<p><strong>.M</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line">@implementation WXApiManager</span><br><span class="line"></span><br><span class="line">#pragma mark - LifeCycle</span><br><span class="line">+(instancetype)sharedManager &#123;</span><br><span class="line">    static dispatch_once_t onceToken;</span><br><span class="line">    static WXApiManager *instance;</span><br><span class="line">    dispatch_once(&amp;onceToken, ^&#123;</span><br><span class="line">        instance = [[WXApiManager alloc] init];</span><br><span class="line">    &#125;);</span><br><span class="line">    return instance;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (void)dealloc &#123;</span><br><span class="line">    self.delegate = nil;</span><br><span class="line">&#125;</span><br><span class="line">// SendAuthReq、SendMessageToWXReq、PayReq</span><br><span class="line">//SendAuthReq == 认证</span><br><span class="line">#pragma mark - WXApiDelegate</span><br><span class="line">- (void)onResp:(BaseResp *)resp &#123;</span><br><span class="line">    if ([resp isKindOfClass:[SendMessageToWXResp class]]) &#123;</span><br><span class="line">        if (_delegate</span><br><span class="line">            &amp;&amp; [_delegate respondsToSelector:@selector(managerDidRecvMessageResponse:)]) &#123;</span><br><span class="line">            SendMessageToWXResp *messageResp = (SendMessageToWXResp *)resp;</span><br><span class="line">            [_delegate managerDidRecvMessageResponse:messageResp];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; else if ([resp isKindOfClass:[SendAuthResp class]]) &#123;</span><br><span class="line">        if (_delegate</span><br><span class="line">            &amp;&amp; [_delegate respondsToSelector:@selector(managerDidRecvAuthResponse:)]) &#123;</span><br><span class="line">            SendAuthResp *authResp = (SendAuthResp *)resp;</span><br><span class="line">            [_delegate managerDidRecvAuthResponse:authResp];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; else if ([resp isKindOfClass:[AddCardToWXCardPackageResp class]]) &#123;</span><br><span class="line">        if (_delegate</span><br><span class="line">            &amp;&amp; [_delegate respondsToSelector:@selector(managerDidRecvAddCardResponse:)]) &#123;</span><br><span class="line">            AddCardToWXCardPackageResp *addCardResp = (AddCardToWXCardPackageResp *)resp;</span><br><span class="line">            [_delegate managerDidRecvAddCardResponse:addCardResp];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;else if([resp isKindOfClass:[PayResp class]])&#123;</span><br><span class="line">        //支付返回结果，实际支付结果需要去微信服务器端查询</span><br><span class="line">//        NSString *strMsg,*strTitle = [NSString stringWithFormat:@&quot;支付结果&quot;];</span><br><span class="line">        </span><br><span class="line">        switch (resp.errCode) &#123;</span><br><span class="line">            case WXSuccess:&#123;</span><br><span class="line">//                strMsg = @&quot;支付结果：成功！&quot;;</span><br><span class="line">                NSLog(@&quot;微信支付成功－PaySuccess，retcode = %d&quot;, resp.errCode);</span><br><span class="line">                // 发通知带出支付成功结果</span><br><span class="line">                [[NSNotificationCenter defaultCenter] postNotificationName:@&quot;WeChatPaySucceed&quot; object:resp];</span><br><span class="line">            &#125;</span><br><span class="line">                break;</span><br><span class="line">                </span><br><span class="line">            default:&#123;</span><br><span class="line">//                strMsg = [NSString stringWithFormat:@&quot;支付结果：失败！retcode = %d, retstr = %@&quot;, resp.errCode,resp.errStr];</span><br><span class="line">                NSLog(@&quot;微信支付失败，retcode = %d, retstr = %@&quot;, resp.errCode,resp.errStr);</span><br><span class="line">                // 发通知带出支付失败结果</span><br><span class="line">                [[NSNotificationCenter defaultCenter] postNotificationName:@&quot;WeChatPayFail&quot; object:resp];</span><br><span class="line">            &#125;</span><br><span class="line">                break;</span><br><span class="line">        &#125;</span><br><span class="line">//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@&quot;OK&quot; otherButtonTitles:nil, nil];</span><br><span class="line">//        [alert show];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (void)onReq:(BaseReq *)req &#123;</span><br><span class="line">    if ([req isKindOfClass:[GetMessageFromWXReq class]]) &#123;</span><br><span class="line">        if (_delegate</span><br><span class="line">            &amp;&amp; [_delegate respondsToSelector:@selector(managerDidRecvGetMessageReq:)]) &#123;</span><br><span class="line">            GetMessageFromWXReq *getMessageReq = (GetMessageFromWXReq *)req;</span><br><span class="line">            [_delegate managerDidRecvGetMessageReq:getMessageReq];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; else if ([req isKindOfClass:[ShowMessageFromWXReq class]]) &#123;</span><br><span class="line">        if (_delegate</span><br><span class="line">            &amp;&amp; [_delegate respondsToSelector:@selector(managerDidRecvShowMessageReq:)]) &#123;</span><br><span class="line">            ShowMessageFromWXReq *showMessageReq = (ShowMessageFromWXReq *)req;</span><br><span class="line">            [_delegate managerDidRecvShowMessageReq:showMessageReq];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; else if ([req isKindOfClass:[LaunchFromWXReq class]]) &#123;</span><br><span class="line">        if (_delegate</span><br><span class="line">            &amp;&amp; [_delegate respondsToSelector:@selector(managerDidRecvLaunchFromWXReq:)]) &#123;</span><br><span class="line">            LaunchFromWXReq *launchReq = (LaunchFromWXReq *)req;</span><br><span class="line">            [_delegate managerDidRecvLaunchFromWXReq:launchReq];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong><a href="https://gitee.com/Min_amdrl/wechatPay" target="_blank" rel="noopener">Demo-手动下载的SDK运行程序崩溃,之后用pod安装,运行成功</a></strong></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/10/25/颜色渐变/" rel="next" title="颜色渐变">
                <i class="fa fa-chevron-left"></i> 颜色渐变
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/04/06/Coredata/" rel="prev" title="Coredata">
                Coredata <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/10.png"
                alt="Erhqe" />
            
              <p class="site-author-name" itemprop="name">Erhqe</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">70</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">31</span>
                  <span class="site-state-item-name">categories</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">tags</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#微信支付"><span class="nav-number">1.</span> <span class="nav-text">微信支付</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Erhqe</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'TiAY2J24ECwQH9uLYsoJpgIK-gzGzoHsz',
        appKey: 'Y9PiVrPXVmfNsv4Aa6cPsADG',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
